package org.KeyCrafter.ds.dict;



import org.KeyCrafter.util.MurmurHash;
import org.KeyCrafter.util.SafeEncoder;

import static org.KeyCrafter.ds.dict.Dict.SEED;


/**
 * hash算法获取哈希值
 */
public class MurmurHash2 implements DictType{
    @Override
    public int hashFunction(Object key) {
        // 只处理String类型的key
        if (key instanceof String) {
            return hash((String)key);
        } else {
            throw new RuntimeException("不是 String 类型的 key");
        }
    }
    @Override
    public boolean keyCompare(Object privdate, Object key1, Object key2) {
        // 只对比String类型的key
        if (key1 instanceof String  && key2 instanceof String)
            return key1.equals(key2);
        else
            throw new RuntimeException("非 String 类型的 key");
    }
    private int hash(String str) {
        byte[] data = SafeEncoder.encode(str);
        return MurmurHash.hash(data, SEED);
    }
}
